package com.zzl.bonds.ticket.service.impl;

import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.UuidUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Query;

import com.zzl.bonds.ticket.dao.ApproveLogDao;
import com.zzl.bonds.ticket.service.ApproveLogService;
import com.zzl.bonds.ticket.entity.ApproveLogEntity;
import com.zzl.bonds.ticket.dto.ApproveLogAddDto;
import com.zzl.bonds.ticket.dto.ApproveLogUpdateDto;

/**
 * 审核记录
 *
 * @author gull
 * @date 2022-09-14 09:31:58
 */
@Service("approveLogService")
public class ApproveLogServiceImpl extends ServiceImpl<ApproveLogDao, ApproveLogEntity> implements ApproveLogService {

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return 分页数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ApproveLogEntity> page = this.page(
                new Query<ApproveLogEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增
     *
     * @param approveLogAddDto 新增实体
     * @param user             当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void saveApproveLog(ApproveLogAddDto approveLogAddDto, User user) throws SysException {
        ApproveLogEntity approveLogEntity = new ApproveLogEntity();
        BeanUtils.copyProperties(approveLogAddDto, approveLogEntity);
        approveLogEntity.setId(UuidUtils.UUID());
        approveLogEntity.setCreatorId(user.getId());
        approveLogEntity.setId(UuidUtils.UUID());
        approveLogEntity.setEnterpriseId(user.getEnterpriseId());
        approveLogEntity.setUpdaterId(user.getId());
        if (!this.save(approveLogEntity)) {
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 修改
     *
     * @param approveLogUpdateDto 修改实体
     * @param user                当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateApproveLogById(ApproveLogUpdateDto approveLogUpdateDto, User user) throws SysException {
        ApproveLogEntity approveLogEntity = new ApproveLogEntity();
        BeanUtils.copyProperties(approveLogUpdateDto, approveLogEntity);
        approveLogEntity.setUpdaterId(user.getId());
        if (!this.updateById(approveLogEntity)) {
            throw new SysException("A00002", "修改失败！");
        }
    }

    /**
     * 根据类型查询审核记录数据
     *
     * @param approveLog 审核记录
     * @return 记录列表
     */
    @Override
    public List<ApproveLogEntity> getApproveLogList(ApproveLogEntity approveLog) {
        QueryWrapper<ApproveLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApproveLogEntity::getApproveType, approveLog.getApproveType())
                .eq(ApproveLogEntity::getBusinessId, approveLog.getBusinessId())
                .orderByDesc(ApproveLogEntity::getCreateTime);
        return this.list(queryWrapper);
    }

}